/* 
 * Javascript to integrate editing functionality into Datables
 * 
 */

/* Add editor buttons to the datatable */
jQuery.fn.dataTableExt.aoFeatures.push( {
    'fnInit': function( oSettings ) {
		// Container for buttons
			var buttons = document.createElement('div');
			buttons.setAttribute('id','editorBtns');
			buttons.setAttribute('class','btn-group');
		// Add Button
			var addbtn = document.createElement('button');
			addbtn.setAttribute('id','add_'+oSettings.sTableId);
			addbtn.setAttribute('class','btn  btn-primary');
			var addLabel = document.createTextNode('Add');
			addbtn.appendChild(addLabel);
			buttons.appendChild(addbtn);
		// Edit Button
			var editbtn = document.createElement('button');
			editbtn.setAttribute('id','edit_'+oSettings.sTableId);
			editbtn.setAttribute('class','btn  btn-primary');
			var editLabel = document.createTextNode('Edit');
			editbtn.appendChild(editLabel);
			buttons.appendChild(editbtn);
		// Delete Button
			var deletebtn = document.createElement('button');
			deletebtn.setAttribute('id','delete_'+oSettings.sTableId);
			deletebtn.setAttribute('class','btn  btn-primary');
			var deleteLabel = document.createTextNode('Delete');
			deletebtn.appendChild(deleteLabel);
			buttons.appendChild(deletebtn);
		// Return the buttons
			return buttons;
    },
    'cFeature': 'E',
    'sFeature': 'Editor'
});

/* Add a new row to the table and display it on the screen by jumping the pagination to the required location */
jQuery.fn.dataTableExt.oApi.fnAddDataAndDisplay = function ( oSettings, aData ) {
    /* Add the data */
    var iAdded = this.oApi._fnAddData( oSettings, aData );
    var nAdded = oSettings.aoData[ iAdded ].nTr;
      
    /* Need to re-filter and re-sort the table to get positioning correct */
    this.oApi._fnReDraw( oSettings );
      
    /* Find it's position in the table */
    var iPos = -1;
    for( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ) {
        if( oSettings.aoData[ oSettings.aiDisplay[i] ].nTr == nAdded ) {
            iPos = i;
            break;
        }
    }
      
    /* Get starting point, taking account of paging */
    if( iPos >= 0 ) {
        oSettings._iDisplayStart = ( Math.floor(i / oSettings._iDisplayLength) ) * oSettings._iDisplayLength;
        this.oApi._fnCalculateEnd( oSettings );
    }
      
    this.oApi._fnDraw( oSettings );
    return {
        'nTr': nAdded,
        'iPos': iAdded
    };
};

/* Get a TD node from a row, taking into account column visibility. */
jQuery.fn.dataTableExt.oApi.fnGetTd  = function ( oSettings, mTr, iTd, bVisOnly ) {
    /* Take either a TR node or aoData index as the mTr property */
    var iRow = (typeof mTr == 'object') ?
        oSettings.oApi._fnNodeToDataIndex(oSettings, mTr) : mTr;
      
    if ( typeof bVisOnly == 'undefined' && !bVisOnly ) {
        /* Looking at both visible and hidden TD elements - convert to visible index, if not present
         * then it must be hidden. Return as appropriate
         */
        var iCalcVis = oSettings.oApi._fnColumnIndexToVisible( oSettings, iTd );
        if ( iCalcVis !== null ) {
            return oSettings.aoData[ iRow ].nTr.getElementsByTagName('td')[ iCalcVis ];
        } else {
            return oSettings.aoData[ iRow ]._anHidden[ iTd ];
        }
    } else {
        /* Only looking at visible TD elements, so just use getElements... */
        return oSettings.aoData[ iRow ].nTr.getElementsByTagName('td')[ iTd ];
    }
};